package net.strudelline.fanorona;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.net.URL;
import java.net.URLClassLoader;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import edu.alaska.uaa.cs401.fgm.FanoronaBoardInterface;
import edu.alaska.uaa.cs401.fgm.FanoronaColor;
import edu.alaska.uaa.cs401.fgm.FanoronaLinkBoard;

public class FanoronaGameBoardFrame extends JPanel implements MouseListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private ImageIcon boardimg;
	private Image cachedboard = null;
	private int cachedheight = -1;
	private int cachedwidth = -1;
	
	final double RE = .12;
	final double RD = .7;
	final double RC = .5;
	final double RB = .3;
	final double RA = .9;
	ImageIcon[] blackicons = new ImageIcon[45];
	Image[] cachedblackicons = new Image[45];
	ImageIcon[] whiteicons = new ImageIcon[45];
	Image[] cachedwhiteicons = new Image[45];
	int blackiconsavailable = 0;
	int whiteiconsavailable = 0;
	
	final double[][][] positions = {	
		{{0.0875,0.8889},{0.1883,0.8957},{0.2930,0.8875},{0.3945,0.8848},{0.5016,0.8875},{0.6078,0.8808},{0.7180,0.8835},{0.8242,0.8835},{0.9224,0.8817}},
		{{0.0891,0.6816},{0.1914,0.6843},{0.2914,0.6789},{0.3945,0.6789},{0.5023,0.6721},{0.6086,0.6707},{0.7156,0.6694},{0.8227,0.6762},{0.9203,0.6775}},
		{{0.0891,0.4892},{0.1906,0.4878},{0.2922,0.4892},{0.3977,0.4892},{0.5008,0.4878},{0.6102,0.4878},{0.7148,0.4892},{0.8195,0.4851},{0.9203,0.4770}},
		{{0.0891,0.3117},{0.1891,0.3117},{0.2922,0.3049},{0.3969,0.3089},{0.5008,0.3062},{0.6070,0.2995},{0.7133,0.2981},{0.8227,0.3049},{0.9211,0.2981}},
		{{0.0898,0.1260},{0.1883,0.1220},{0.2953,0.1260},{0.3992,0.1233},{0.5023,0.1206},{0.6070,0.1206},{0.7109,0.1206},{0.8211,0.1179},{0.9188,0.1179}},
	};
//	double positions[][][] = new double[][][] {
//			{{.1,RA},{.2,RA},{.3,RA},{.4,RA},{.5,RA},{.6,RA},{.7,RA},{.8,RA},{.9,RA}},
//			{{.1,RB},{.2,RB},{.3,RB},{.4,RB},{.5,RB},{.6,RB},{.7,RB},{.8,RB},{.9,RB}},
//			{{.1,RC},{.2,RC},{.3,RC},{.4,RC},{RC,RC},{.6,RC},{.7,RC},{.8,RC},{.9,RC}},
//			{{.1,RD},{.2,RD},{.3,RD},{.4,RD},{.5,RD},{.6,RD},{.7,RD},{.8,RD},{.9,RD}},
//			{{.1,RE},{.2,RE},{.3,RE},{.4,RE},{.5,RE},{.6,RE},{.7,RE},{.8,RE},{.9,RE}},
//			//			{{.1,#},{.2,#},{RD,#},{.4,#},{.5,#},{.6,#},{.7,#},{.8,#},{.9,#}}
//
//	};
	
	private FanoronaBoardInterface board = null;
	
	public FanoronaGameBoardFrame() {
		URLClassLoader loader = (URLClassLoader)this.getClass().getClassLoader();
		boardimg = new ImageIcon(loader.findResource("net/strudelline/fanorona/media/gameboard.png"));
		int blackcount = 0;
		int whitecount = 0;
		for (int i = 0; i < 45; i++) {
			URL url = loader.findResource(String.format("net/strudelline/fanorona/media/black %d.png",i+1));
			ImageIcon img = null;
			if (url == null) break;
			img = new ImageIcon(url);
			blackicons[i] = img;
			blackcount++;
		}
		for (int i = 0; i < 45; i++) {
			ImageIcon img = null;
			URL url = loader.findResource(String.format("net/strudelline/fanorona/media/white %d.png",i+1));
			if (url == null) break;
			img = new ImageIcon(url);
			whiteicons[i] = img;
			whitecount++;
		}
		System.out.format("Found %d black and %d white\n",blackcount,whitecount);
		//JLabel background = new JLabel();
		//background.setIcon(new ImageIcon(boardimg.getImage().getScaledInstance(500, 500, Image.SCALE_SMOOTH)));
		//add(background);
		//boardimg.getImage()
		setPreferredSize(new Dimension(450,246));
		//board = new FanoronaLinkBoard();
		this.addMouseListener(this);
	}
	public void mouseClicked(MouseEvent arg0) {
	}

	public void mouseEntered(MouseEvent arg0) {
	}

	public void mouseExited(MouseEvent arg0) {
	}

	public void mousePressed(MouseEvent arg0) {
	}

	public void mouseReleased(MouseEvent arg0) {
		System.out.format("%.4f,%.4f\n",(double)arg0.getX()/((double)getWidth()),(double)arg0.getY()/((double)getHeight()));
	}
	
	public void setBoard(FanoronaBoardInterface b) {
		this.board = b;
		final FanoronaGameBoardFrame thiss = this;
		//thiss.invalidate();
		//thiss.revalidate();
		thiss.repaint();
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//
//			}
//		});
	}
	
	//public void paintComponent(Graphics g) {
	public void paintComponent(Graphics g) {
		int width = getWidth();
		int height = getHeight();
		double windowaspect = ((double)width)/((double)height);
		// as height increases, aspect decreases
		double imgwidth = boardimg.getIconWidth();
		double imgheight = boardimg.getIconHeight();
		double aspect = imgwidth/imgheight;
		if (windowaspect < aspect) { // too much height
			height = (int) (((double)width) / aspect);
		} else { // too much width
			width = (int) (((double)height) * aspect);
		}

		/*
		 * This area is setting the size of the icons.  Change bubblesize to affect change.
		 */
		double bubblesize = .12;
		int bubblewidth = (int) (width * bubblesize);
		int bubbleheight = (int) (height * bubblesize);
		if (bubblewidth > bubbleheight) {
			bubblewidth = bubbleheight;
		} else {
			bubbleheight = bubblewidth;
		}

		//System.out.format("%dx%d\n",width,height);
		//System.out.println()
		if (cachedboard == null || width != cachedwidth || height != cachedheight) {
			if (cachedboard != null) cachedboard.flush();
			cachedboard = boardimg.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
			cachedwidth = width;
			cachedheight = height;
			whiteiconsavailable = 0;
			blackiconsavailable = 0;
			for (int i = 0; i < 45; i++) {
				if (cachedblackicons[i] != null) {
					cachedblackicons[i].flush();
				}
				if (blackicons[i] != null) {
					cachedblackicons[i] = blackicons[i].getImage().getScaledInstance(bubblewidth,bubbleheight,Image.SCALE_FAST);
					blackiconsavailable++;
				}
				if (cachedwhiteicons[i] != null) {
					cachedwhiteicons[i].flush();
				}
				if (whiteicons[i] != null) {
					whiteiconsavailable++;
					cachedwhiteicons[i] = whiteicons[i].getImage().getScaledInstance(bubblewidth,bubbleheight,Image.SCALE_FAST);
				}
			}
		}
		//Image img = boardimg.getImage();
		//img.
		g.drawImage(cachedboard,0,0,this);/*new ImageObserver() {
		//img.paintIcon(this,g,0,0);

			@Override
			public boolean imageUpdate(Image arg0, int arg1, int arg2,
					int arg3, int arg4, int arg5) {
				// TODO Auto-generated method stub
				return false;
			}
			
		});*/
		
		//System.out.format("%d black and %d white\n",blackiconsavailable,whiteiconsavailable);

		int hbw = bubblewidth >> 1;
		int hbh = bubbleheight >> 1;
		
		if (board != null) {
			for (int x = 0; x < 9;x++) {
				for (int y = 0; y < 5; y++) {
					int midx = (int) ((double)width * (positions[y][x][0]));
					int midy = (int) ((double)height * (positions[y][x][1]));

					FanoronaColor cell = board.getCellColor(x,y);
					if (cell != null) {
						switch (cell) {
						case White:
							if (whiteiconsavailable < 1) {
								g.setColor(Color.white);
								g.fillOval(midx-hbw, midy-hbh, bubblewidth, bubbleheight);
							} else {
								g.drawImage(cachedwhiteicons[(x*y)%whiteiconsavailable],midx-hbw,midy-hbh,this);
								//g.drawRect(midx-hbw, midy-hbh, bubblewidth, bubbleheight);
							}
							break;
						case Black:
							if (blackiconsavailable < 1) {
								g.setColor(Color.black);
								g.fillOval(midx-hbw,midy-hbh,bubblewidth,bubbleheight);
							} else {
								g.drawImage(cachedblackicons[(x*y)%blackiconsavailable],midx-hbw,midy-hbh,this);
								//g.drawRect(midx-hbw, midy-hbh, bubblewidth, bubbleheight);
							}
							break;
						}
					}
				}
			}
		}
	}
	
	
	
	static public void main(String args[]) {
		final JFrame frame = new JFrame();
		frame.setTitle("Fanorona Board Test");
		frame.setSize(500, 500);
		frame.getContentPane().setLayout(new BorderLayout());
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		FanoronaGameBoardFrame bp = new FanoronaGameBoardFrame();
		frame.add(bp,"Center");
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				frame.setVisible(true);
				frame.pack();
			}
		});
		//frame.pack();
	}
}
